﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BonsaiScrum.Mvc.Models;
using BonsaiScrum.Bus;
using BonsaiScrum.Core.Command;
using BonsaiScrum.Core.Persistance;
using BonsaiScrum.Mvc.Filters;
using BonsaiScrum.Core.Domain;

namespace BonsaiScrum.Mvc.Controllers
{
    [Authorize]
    public class StoryController : Controller
    {
        private ICommandBus _commandBus;
        private IProjectReportingRepository _projectReportingRepository;

        public StoryController(ICommandBus commandBus, IProjectReportingRepository projectReportingRepository)
        {
            _commandBus = commandBus;
            _projectReportingRepository = projectReportingRepository;
        }

        public ActionResult List(int projectId)
        {
            var project = _projectReportingRepository.GetProjectWithId(projectId);

            return View(project);
        }

        public ActionResult Create(int projectId)
        {
            var model = new CreateStoryModel
            {
                ProjectID = projectId
            };

            return View(model);
        }

        [HttpPost]
        [Role(typeof(ScrumMaster))]
        public ActionResult Create(CreateStoryModel model)
        {
            if (ModelState.IsValid)
            {
                var createNewStoryCommand = new CreateNewStory(model.StoryDescription, model.ProjectID, 1);
                _commandBus.Publish(createNewStoryCommand);

                return View("CreateNewStorySuccess", model.ProjectID);
            }

            return View(model);
        }

        public ActionResult Remove(int id)
        {
            var removeStoryCommand = new RemoveStoryFromProject(id);
            _commandBus.Publish(removeStoryCommand);

            return View("RemoveStorySuccess");
        }
    }
}
